/**
 * Copyright 2009 Henning Teek, Ingo Renner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.bricket.plugin.quickstart.web.helloworld;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.odlabs.wiquery.ui.themes.ThemeUiHelper.IconEnum;

import com.googlecode.bricket.plugin.quickstart.domain.HelloWorld;
import com.googlecode.bricket.repository.GenericDomainService;
import com.googlecode.bricket.web.common.BricketAjaxButton;
import com.googlecode.bricket.web.common.BricketAjaxLink;
import com.googlecode.bricket.web.common.BricketPanel;
import com.googlecode.bricket.web.common.ValidationStyleBehavior;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@AuthorizeInstantiation("ROLE_USER")
public class EditOrCreateHelloWorldPanel extends BricketPanel {
	@SpringBean
	private GenericDomainService genericDomainService;

	private Form<HelloWorld> form;

	public EditOrCreateHelloWorldPanel(String id) {
		this(id, new HelloWorld());
	}

	public EditOrCreateHelloWorldPanel(String id, HelloWorld hw) {
		super(id);
		CompoundPropertyModel<HelloWorld> model = new CompoundPropertyModel<HelloWorld>(hw);

		form = new Form<HelloWorld>("helloworld.form", model);
		StringResourceModel lDesc = new StringResourceModel("description.label", EditOrCreateHelloWorldPanel.this, null);
		form.add(new Label("description.label", lDesc));
		form.add(new TextField<String>("description")
				.setRequired(true)
				.setLabel(lDesc)
				.add(new ValidationStyleBehavior()));
		add(form);
	
		setHeadline(new StringResourceModel("form.headline", EditOrCreateHelloWorldPanel.this, null));

		addButton(new BricketAjaxLink(new StringResourceModel("cancel.label", EditOrCreateHelloWorldPanel.this, null), IconEnum.CANCEL) {
			@Override
			public void onClick(AjaxRequestTarget target) {
				getSession().cleanupFeedbackMessages();
				returnToManagement(target);
			}
		});
		
		addButton(new BricketAjaxButton(new StringResourceModel("submit.label", EditOrCreateHelloWorldPanel.this, null), IconEnum.CHECK, form) {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				genericDomainService.save(HelloWorld.class, (HelloWorld)form.getModelObject());
				form.info(new StringResourceModel("save.success", EditOrCreateHelloWorldPanel.this, null).getString());
				returnToManagement(target);
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.addComponent(getFeedback());
			}
		});
	}

	private void returnToManagement(AjaxRequestTarget target) {
		final Panel p = new ManageHelloWorldPanel(EditOrCreateHelloWorldPanel.this.getId());
		p.setOutputMarkupId(true);
		EditOrCreateHelloWorldPanel.this.getParent().addOrReplace(p);
		target.addComponent(p);
		target.addComponent(getFeedback());
	}
}
